﻿// 將此頁的btn設為選中 ID由asp.net生成 
$('#ContentPlaceHolder1_UserPageLinkList1_linkUserPublishedRecord').addClass('active');
$(function () {
    var PaginationPAGEROW = 20;
    var PaginationItem = 5;
    var PaginationDiv = "pagination";
    var PaginationSpan = "PRPaginationSpan";
    var PaginationTable = "TablePR";
    var Ajax = "./Handler/HandlerUserPublishedRecord.ashx";
    var AjaxType = 2;
    /*分頁選單初始化*/
    Pagination(
        PaginationSpan + "1", PaginationPAGEROW, PaginationDiv
        , PaginationSpan, PaginationTable, PaginationItem
        , Ajax, AjaxType);
    /*分頁選單Clicke選單與表更新*/
    PaginationClick(
        PaginationDiv, PaginationSpan, PaginationTable, PaginationPAGEROW
        , PaginationItem, Ajax, AjaxType);
    var page;
    var strpage = "page=";
    if (window.location.href.indexOf("page") != -1) {
        page = window.location.href.substr(window.location.href.indexOf("page") + strpage.length);
        Pagination2(
            PaginationSpan + "1"
            , PaginationPAGEROW
            , PaginationDiv
            , PaginationSpan
            , PaginationTable
            , PaginationItem
            , Ajax
            , AjaxType
            , PaginationPAGEROW
            , page
            );
    } else {
        page = 1;
        /*讀取發表紀錄*/
        UserPublishedRecord(page, PaginationPAGEROW, PaginationTable);
    }
    /*選單特效*/
    $(".TablePagination").on("mouseenter", "tbody tr", function () {
        $(this).css("background-color", "rgba(0,153,255,0.2)");
    });
    $(".TablePagination").on("mouseleave", "tbody tr", function () {
        $(this).css("background-color", "#ffffff");
    });
});
/*讀取發表紀錄*/
function UserPublishedRecord(PageNum, Pagination, PaginationTable) {
    $.ajax({
        url: "./Handler/HandlerUserPublishedRecord.ashx"
        , type: "post"
        , data: "type=1"
         + "&PageNum=" + PageNum
        + "&Pagination=" + Pagination
        , success: function (data) {
            var i = 1;
            $.each(data, function (name, value) {
                $.each(value, function () {
                    /*回傳===========================================*/
                    $("#" + PaginationTable).append("<tr id='tr" + i + "'><td>" + (i + (PageNum - 1) * Pagination) + "</td><td>"
                        + this.date + "</td><td>"
                        + this.title + "</td><td><a href=./UploadPage.aspx?target="
                        + this.uid + "&page=" + PageNum + "><p class='text-warning'>修改文章</p></a></td></tr>"
                        //+ this.uid + "&page=" + PageNum + "><p class='text-warning'>修改文章</p></a></td><td class='UPRDelete' id="
                        //+ this.uid + ">刪除文章</td></tr>"
                        );
                    $("#tr" + i).hide().delay(i * 25).fadeIn(500);
                    /*回傳===========================================*/
                    i++;
                });
            });
            /*successEnd===========================================*/
        }
        , error: function () {
            alert("請勿亂輸入");
        }
    });
}
/*防止亂輸入*/
function Pagination2(CurrentlyPage, Row, PaginationDIV, PaginationSpan, PaginationTable, SpanNum, URL, AjaxType, PaginationPAGEROW, page) {
    $.ajax({/*取資料筆數*/
        url: URL
        , type: "post"
        , data: "type=" + AjaxType
        , success: function (data) {
            $.each(data, function (name, value) {
                $.each(value, function () {
                    /*回傳===========================================*/
                    if (page >= this.num) {
                        page = 1;
                        /*讀取發表紀錄*/
                        UserPublishedRecord(page, PaginationPAGEROW, PaginationTable);
                    }
                    else {
                        /*讀取發表紀錄*/
                        UserPublishedRecord(page, PaginationPAGEROW, PaginationTable);
                    }
                    /*回傳===========================================*/
                });
            });
            /*successEnd===========================================*/
        }
    });
}
/*分頁*/
function Pagination(CurrentlyPage, Row, PaginationDIV, PaginationSpan, PaginationTable, SpanNum, URL, AjaxType) {
    //現在頁數,每頁幾列,裝載分頁DIV ID,裝載數字SPAN ID,分頁目標名稱,分頁選單數(一定要奇數,最小3),取資料筆數URL
    $.ajax({/*取資料筆數*/
        url: URL
        , type: "post"
        , data: "type=" + AjaxType
        , success: function (data) {
            $.each(data, function (name, value) {
                $.each(value, function () {
                    /*回傳===========================================*/
                    /*參數準備*/
                    var CurrentlyPage2 = parseInt(CurrentlyPage.substr(PaginationSpan.length));
                    //CurrentlyPage目前所在頁數,避免傳字串parseInt()
                    var TableRows = Row;  // 每頁要顯示幾列
                    var DBDataNum = this.num; // 資料筆數
                    var TablePages = Math.floor(DBDataNum / TableRows);// 計算頁數
                    var mod = DBDataNum % TableRows; // 餘數
                    if (mod > 0) { TablePages++; } // 不能整除
                    if (CurrentlyPage2 == 0) { CurrentlyPage2 = TablePages }//最後一頁處理
                    if (CurrentlyPage2 == -1) { CurrentlyPage2 = 1 }//第一頁處理
                    /*選單計算*/
                    if (CurrentlyPage2 - (SpanNum - 1) / 2 > 0) {
                        var StartItem = CurrentlyPage2 - (SpanNum - 1) / 2;
                    }
                    else {
                        var StartItem = 1;
                    }
                    if ((CurrentlyPage2 + (SpanNum - 1) / 2 <= TablePages) && ((CurrentlyPage2 + (SpanNum - 1) / 2) > 0)) {
                        var EndItem = CurrentlyPage2 + (SpanNum - 1) / 2;
                    }
                    else {
                        var EndItem = TablePages;
                    }
                    /*實作選單*/
                    $('#' + PaginationDIV).append('<li id=' + PaginationSpan + '-' + 1 + '>' + '<a>' + '首頁' + '</a>' + '</li>').fadeIn(500);
                    for (i = StartItem; i <= EndItem; i++) {
                        $('#' + PaginationDIV).append('<li id=' + PaginationSpan + i + '>' + '<a>' + i + '</a>' + '</li>').fadeIn(500);
                    }
                    $('#' + PaginationDIV).append('<li id=' + PaginationSpan + 0 + '>' + '<a>' + '最後一頁' + '</a>' + '</li> ').fadeIn(500);
                    $('#' + PaginationSpan + CurrentlyPage2).css('color', 'red');

                    /*回傳===========================================*/
                });
            });
            /*successEnd===========================================*/
        }
    });
}
/*分頁選單Clicke選單與表更新*/
function PaginationClick(PaginationDIV, PaginationSpan, PaginationTable, Row, SpanNum, URL, AjaxType) {
    $.ajax({/*取資料筆數*/
        url: URL
        , type: "post"
        , data: "type=" + AjaxType
        , success: function (data) {
            $.each(data, function (name, value) {
                $.each(value, function () {
                    /*回傳===========================================*/
                    /*參數準備*/
                    //CurrentlyPage目前所在頁數,避免傳字串parseInt()
                    var TableRows = Row;  // 每頁要顯示幾列
                    var DBDataNum = this.num; // 資料筆數
                    var TablePages = Math.floor(DBDataNum / TableRows);// 計算頁數
                    var mod = DBDataNum % TableRows; // 餘數
                    if (mod > 0) { TablePages++; } // 不能整除
                    $('#' + PaginationDIV).on("click", "li", function (event) {
                        var ClickPage = this.id.substr(PaginationSpan.length);
                        if (ClickPage == 0) { ClickPage = TablePages }//最後一頁處理
                        if (ClickPage == -1) { ClickPage = 1 }//第一頁處理
                        $("#" + PaginationTable + " tbody").html("");
                        $('#' + PaginationDIV).html("");
                        UserPublishedRecord(ClickPage, Row, PaginationTable);
                        Pagination(
                        this.id
                        , Row
                        , PaginationDIV
                        , PaginationSpan
                        , PaginationTable
                        , SpanNum
                        , URL
                        , AjaxType
                        );
                        return false;
                    });
                    /*回傳===========================================*/
                });
            });
            /*successEnd===========================================*/
        }
    });
}